<?php

class item extends baseModel {

    private static $instance;

    function __construct() {
        parent::__construct();
    }

    public static function newInstance() {
        if (!self::$instance instanceof self) {
            self::$instance = new self ();
        }
        return self::$instance;
    }

    function getTableName() {
        return 'item';
    }

    function getDescriptionTableName() {
        return 'item_description';
    }

    function getLocationTableName() {
        return 'item_location';
    }

    function getCommentTableName() {
        return 'item_comment';
    }

    function getResourceTableName() {
        return 'item_resource';
    }

    function deleteByID($ID) {
        if ($ID == '') {
            return false;
        }
        $args[':item_id'] = $ID;
        $this->beginTrans();

        $sql = sprintf('delete from %s where item_id=:item_id', $this->getTableName());

        if (!$this->query($sql, $args)) {
            $this->rollback();
            return false;
        }

        if (!$this->query($sql, $args)) {
            $this->rollback();
            return false;
        }

        $sql = array();

        $sql[] = sprintf('delete from %s where item_id=:item_id', $this->getLocationTableName());
        $sql[] = sprintf('delete from %s where item_id=:item_id', $this->getCommentTableName());
        $sql[] = sprintf('delete from %s where item_id=:item_id', $this->getResourceTableName());

        foreach ($sql as $key => $value) {
            $this->query($value, $args);
        }

        $this->commit();
    }

    function enable($args) {
        if (!is_array($args)) {
            return false;
        }
        if (count($args) < 1) {
            return false;
        }

        $fields = array();
        $fields['enabled'] = 1;

        $result = false;

        foreach ($args as $key => $value) {
            $temp_args = array();
            $temp_args[$key] = $value;
            $result = $this->update($temp_args, $fields);
        }
        return $result;
    }

    function findByCategoryID(array $args) {
        if (!is_array($args)) {
            return false;
        }

        if (count($args) < 1) {
            return false;
        }
        $column = 'category_id';
        $value = implode(',', $args);

        $sql = sprintf('SELECT * FROM %s WHERE %s in (%s)', $this->getTableName(), $column, $value);
        $result = $this->con->query($sql);
    }

    function findByCategorySlug($slug=array()) {
        if (!is_array($slug)) {
            return false;
        }

        $args = array();
        foreach ($slug as $key => $value) {
            $result = categories::newInstance()->findBySlug($value);
            if ($result) {
                $args[] = $result[0]['category_id'];
            }
        }
        unset($slug);
        return $this->findByCategoryID($args);
    }

    

    function getByLocationSlug($slug) {
        if ($slug == '') {
            return false;
        }
    }

    function getByCountryCode($code) {
        if ($code == '') {
            return false;
        }
    }

    function getByCountrySlug($slug) {
        if ($slug == '') {
            return false;
        }
    }

    function getByRegionID($ID) {
        if ($ID == '') {
            return false;
        }
    }

    function getByRegionSlug($slug) {
        if ($slug == '') {
            return false;
        }
    }

    function getByCityID($ID) {
        if ($ID == '') {
            return false;
        }
    }

    function getByCitySlug($slug) {
        if ($slug == '') {
            return false;
        }
    }

    function getByCityAreaID($ID) {
        if ($ID == '') {
            return false;
        }
    }

    function getByCityAreaSlug($slug) {
        if ($slug == '') {
            return false;
        }
    }

    function getByCondition($args) {
        if (!is_array($args)) {
            return false;
        }
        if (count($args) < 1) {
            return false;
        }
    }

    function findBySlug($args=array()) {
        if (!is_array($args)) {
            return false;
        }
        if (count($args) < 1) {
            return false;
        }
        foreach ($args as $key => $value) {
            $args[$key] = "'" . $value . "'";
        }
        $column = 'slug';
        $value = implode(',', $args);
        $sql = sprintf('SELECT * FROM %s WHERE %s in (%s)', $this->getTableName(), $column, $value);
        //echo $sql;
        return $this->con->query($sql);
    }

    function extend($items) {
        if (!is_array($items)) {
            return false;
        }

        $item_id = array();
        
        $old_items=array();

        foreach ($items as $key => $value) {
            $old_items[$value['item_id']]=$value;
            $item_id[] = $value['item_id'];
        }
        unset($items);
        
        
        
        
    }

}

?>